package  {
	import org.flixel.*;
	
	public class ClickEye extends FlxGroup {
		public var dilation:Number = 1;
		public var eye:FlxSprite = new FlxSprite(50, 58);
		
		public function ClickEye() {
			super();
			this.scrollFactor = new FlxPoint(0, 0);
			
			this.eye.loadGraphic(ClickEyeImg);
			this.eye.scrollFactor = this.scrollFactor;
			//this.eye.alpha = 0.65;
			//this._eyeBrow.alpha = 0.4;
			//this._eyePupil.alpha = 0.65;
			//this._eyeHighlight.alpha = 0.65;
			
			this._brow.loadGraphic(ClickEyeBrowImg);
			this._brow.x = this.eye.x - 2;
			this._brow.y = this.eye.y - 26;
			this._brow.scrollFactor = this.scrollFactor;
			
			this._pupil.loadGraphic(ClickEyePupilImg);
			this._pupil.x = this.eye.x + 28;
			this._pupil.y = this.eye.y + 9;
			this._pupil.scrollFactor = this.scrollFactor;
			
			this._highlight.loadGraphic(ClickEyeHighlightImg);			
			this._highlight.x = this.eye.x;
			this._highlight.y = this.eye.y;
			this._highlight.scrollFactor = this.scrollFactor;
			
			this.add(this.eye);
			this.add(this._brow);
			this.add(this._pupil);
			this.add(this._highlight);
		}
		
		override public function update():void {
			if (!this.exists) return;
			this.dilation -= this._constrictSpeed * 
				(10) * 
				FlxG.elapsed;
			if (this.dilation <= 0) {
				if (!this._isTripping)
					FlxG.playMusic(Game.TripMusic);
				this._isTripping = true;
				this.dilation = 0;
			}
			if (FlxG.mouse.justPressed() && 
				this.eye.overlapsPoint(
					FlxG.mouse.screenX + FlxG.scroll.x, 
					FlxG.mouse.screenY + FlxG.scroll.y))
			{
				this._isTripping = false;
				FlxG.mouse.reset();
				FlxG.play(EyePokeSound);
				if (this.dilation <= 0)
					FlxG.playMusic(Game.NormalMusic);
				this.dilation += this._dilateSpeed * FlxG.elapsed;	
			}
			if (this.dilation > this._maxDilation) 
				this.dilation = this._maxDilation;
			this._pupil.scale.x = this.dilation + 0.1;
			this._pupil.scale.y = this.dilation + 0.1;
			for each(var member:FlxObject in this.members) {
				member.x += (this.x - this._last.x);
				member.y += (this.y - this._last.y);
			}
			super.update();
		}
		
		private var _isTripping:Boolean = false;
		private var _maxDilation:Number = 0.92;
		private var _dilateSpeed:Number = 10;
		private var _constrictSpeed:Number = 0.01;
		private var _brow:FlxSprite = new FlxSprite();
		private var _pupil:FlxSprite = new FlxSprite();
		private var _highlight:FlxSprite = new FlxSprite();
		[Embed(source = '../content/ClickEye.png')] 
			private var ClickEyeImg:Class;
		[Embed(source = '../content/ClickEyeBrow.png')] 
			private var ClickEyeBrowImg:Class;
		[Embed(source = '../content/ClickEyeHighlight.png')] 
			private var ClickEyeHighlightImg:Class;
		[Embed(source = '../content/ClickEyePupil.png')] 
			private var ClickEyePupilImg:Class;
		// embedded sounds
		[Embed(source = '../content/Eye Poke.mp3')] 
			private var EyePokeSound:Class;
	}

}